package com.factory.rental.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.factory.rental.dto.CleaningRecordDTO;
import com.factory.rental.mapper.CleaningRecordMapper;
import com.factory.rental.model.CleaningRecord;
import com.factory.rental.model.Room;
import com.factory.rental.service.CleaningRecordService;
import com.factory.rental.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 清洁记录服务实现类
 */
@Service
public class CleaningRecordServiceImpl extends ServiceImpl<CleaningRecordMapper, CleaningRecord> implements CleaningRecordService {

    private final RoomService roomService;

    @Autowired
    public CleaningRecordServiceImpl(RoomService roomService) {
        this.roomService = roomService;
    }

    @Override
    @Transactional
    public CleaningRecord saveCleaningRecord(CleaningRecordDTO cleaningRecordDTO) {
        CleaningRecord cleaningRecord = new CleaningRecord();
        convertToEntity(cleaningRecordDTO, cleaningRecord);
        save(cleaningRecord);
        return cleaningRecord;
    }

    @Override
    public Optional<CleaningRecord> getCleaningRecordById(Long id) {
        return Optional.ofNullable(getById(id));
    }

    @Override
    public List<CleaningRecord> getCleaningRecordsByRoomId(Long roomId) {
        return lambdaQuery()
                .eq(CleaningRecord::getRoom, roomId)
                .list();
    }

    @Override
    public List<CleaningRecord> getCleaningRecordsByCleanerId(String cleanerId) {
        return lambdaQuery()
                .eq(CleaningRecord::getCleanerId, cleanerId)
                .list();
    }

    @Override
    public List<CleaningRecord> getCleaningRecordsByStatus(CleaningRecord.CleaningStatus status) {
        return lambdaQuery()
                .eq(CleaningRecord::getStatus, status)
                .list();
    }

    @Override
    public Page<CleaningRecord> getAllCleaningRecords(Pageable pageable) {
        return page(pageable);
    }

    @Override
    @Transactional
    public CleaningRecord updateCleaningRecord(Long id, CleaningRecordDTO cleaningRecordDTO) {
        CleaningRecord cleaningRecord = getById(id);
        if (cleaningRecord == null) {
            throw new RuntimeException("未找到指定ID的清洁记录");
        }
        convertToEntity(cleaningRecordDTO, cleaningRecord);
        updateById(cleaningRecord);
        return cleaningRecord;
    }

    @Override
    @Transactional
    public void deleteCleaningRecord(Long id) {
        if (!removeById(id)) {
            throw new RuntimeException("删除清洁记录失败");
        }
    }

    @Override
    @Transactional
    public void updateCleaningRecordStatus(Long id, CleaningRecord.CleaningStatus status) {
        CleaningRecord cleaningRecord = getById(id);
        if (cleaningRecord == null) {
            throw new RuntimeException("未找到指定ID的清洁记录");
        }
        cleaningRecord.setStatus(status);
        updateById(cleaningRecord);
    }

    @Override
    @Transactional
    public void inspectCleaningRecord(Long id, Integer score, String notes) {
        CleaningRecord cleaningRecord = getById(id);
        if (cleaningRecord == null) {
            throw new RuntimeException("未找到指定ID的清洁记录");
        }
        cleaningRecord.setStatus(CleaningRecord.CleaningStatus.INSPECTED);
        cleaningRecord.setScore(score);
        cleaningRecord.setNotes(notes);
        updateById(cleaningRecord);
    }

    /**
     * 将DTO转换为实体类
     * @param dto DTO对象
     * @param entity 实体类对象
     */
    private void convertToEntity(CleaningRecordDTO dto, CleaningRecord entity) {
        if (dto.getId() != null) {
            entity.setId(dto.getId());
        }
        if (dto.getRoomId() != null) {
            Room room = roomService.getRoomById(dto.getRoomId())
                    .orElseThrow(() -> new RuntimeException("未找到指定ID的房间"));
            entity.setRoom(room);
        }
        entity.setCleanerName(dto.getCleanerName());
        entity.setCleanerId(dto.getCleanerId());
        entity.setStartTime(dto.getStartTime());
        entity.setEndTime(dto.getEndTime());
        entity.setStatus(dto.getStatus());
        entity.setBeforePhoto(dto.getBeforePhoto());
        entity.setAfterPhoto(dto.getAfterPhoto());
        entity.setScore(dto.getScore());
        entity.setNotes(dto.getNotes());
        entity.setGpsLocation(dto.getGpsLocation());
    }
}